<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Account\FrontUser;
use AuctionCore\Broker\Commission;
use AuctionCore\System\AuctionException;
use AuctionCore\Common\Page;

/**
 * 红包管理
 * @author Xmh
 */
class CommissionController extends BaseController {

    public function __construct() {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
    }

    public function index() {
        $this->display();
    }

    /**
     * 红包列表
     */
    public function commission_list() {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['c_id'] = I('get.c_id/d');
        $params['c_type'] = I('get.c_type/d');
        $params['agent_uid'] = I('get.agent_uid/d');
        $params['agent_phone'] = I('get.agent_phone');
        $params['car_no'] = I('get.car_no');
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        $result = Commission::get_commission_list($page, $size, $params);
//        dump($result);exit;
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign('total',$result['total']);
        $this->assign('commission_total',$result['commsission_total']);
        $this->display();
    }
    /**
     * 红包规则页
     */
    public function rule_config(){
        $direct_packet_conf = \AuctionCore\System\SystemConf::getConfByName("DIRECT_PACKET_DEAL");
        $direct_packet_conf = json_decode($direct_packet_conf['value'],true);
        $data['amount'] = $direct_packet_conf['amount'];
        $data['agent_per'] = $direct_packet_conf['rate'] * 100;
        $data['superior_per'] = 100 -  $direct_packet_conf['rate'] * 100;
        $this->assign('data',$data);
        $this->display();
    }
    /**
     * 更新红包分成比例规则
     */
    public function update_rule_config(){
        $commission_money = I('post.commission_money');
        $agent_per = I('post.agent_per');
        $superior_per = I('post.superior_per');
        if(empty($commission_money)){
            $this->helpjsonresult("400",'红包金额不能为空');
        }
        $money_match = "/(^[0-9]\d*$)/";
        if(!preg_match($money_match,$commission_money)){
            $this->helpjsonresult("400",'红包金额格式不正确');
        }
        if(empty($agent_per)){
            $this->helpjsonresult("400",'经纪人占比不能为空');
        }
        $per_match = "/^(?:0|[1-9][0-9]?|100)$/";
        if(!preg_match($per_match,$agent_per)){
            $this->helpjsonresult("400",'经纪人占比不正确');
        }
        if(empty($superior_per)){
            $this->helpjsonresult("400",'上级占比不能为空');
        }
        if(!preg_match($per_match,$superior_per)){
            $this->helpjsonresult("400",'上级占比不正确');
        }
        $per = $agent_per + $superior_per;
        if($per != 100){
            $this->helpjsonresult("400",'分成比例设置不正确');
        }
        $data['amount'] = $commission_money;
        $data['rate'] = $agent_per / 100;
        $value = json_encode($data);
        $res = \AuctionCore\System\SystemConf::updateConfByValue("DIRECT_PACKET_DEAL",$value);
        if($res === false){
            $this->helpjsonresult('500','更新失败');
        }
        $this->helpjsonresult('200','更新成功');
    }
}
